package com.chainyoung.component_tmtrade.ui.view;

import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.chainyoung.common.utils.ConvertUtils;
import com.chainyoung.common.utils.ScreenUtils;
import com.chainyoung.component_tmtrade.R;
import com.chainyoung.component_tmtrade.R2;
import com.chainyoung.component_tmtrade.ui.adapter.SelectOrderStatusAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyOrderFragment.ORDER_CURRENT;
import static com.chainyoung.component_tmtrade.ui.fragment.fiatcurrency.TradeFiatCurrencyOrderFragment.ORDER_HISTORY;

public class SelectOrderStatusWindow extends PopupWindow {


    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.view)
    View view;


    private Context mContext;
    private Unbinder unbinder;
    private SelectOrderStatusAdapter adapter;
    private OnSelectStatus onSelectCategory;
    private List<String> statusList = new ArrayList<>();
    private List<Integer> status = new ArrayList<>();
    private String listType;


    public SelectOrderStatusWindow(Context context, String listType) {
        this.mContext = context;
        View windowView = LayoutInflater.from(mContext).inflate(R.layout.layout_order_status_window, null);
        setContentView(windowView);
        unbinder = ButterKnife.bind(this, windowView);
        this.listType = listType;
        int appWidth = ScreenUtils.getScreenWidth(mContext);
        this.setWidth(appWidth);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setOutsideTouchable(true);
        this.setFocusable(true);
        this.setBackgroundDrawable(new BitmapDrawable());
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        //this.setAnimationStyle(R.style.popuwindow);
        initData();
        initView();
    }

    private void initData() {

        switch (listType) {
            case ORDER_CURRENT:
                statusList.add(mContext.getString(R.string.tmtrade_all));
                statusList.add(mContext.getString(R.string.tmtrade_order_wait_for_pay));
                statusList.add(mContext.getString(R.string.tmtrade_order_already_payed));
                statusList.add(mContext.getString(R.string.tmtrade_wait_for_recieve));
                status.add(-1);
                status.add(10);
                status.add(7);
                status.add(8);
                break;
            case ORDER_HISTORY:
                statusList.add(mContext.getString(R.string.tmtrade_all));
                statusList.add(mContext.getString(R.string.tmtrade_order_finished));
                statusList.add(mContext.getString(R.string.tmtrade_order_closed));
                status.add(-1);
                status.add(1);
                status.add(234);
                break;
        }
    }

    private void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new SelectOrderStatusAdapter(mContext,statusList);
        rvList.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        adapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p = (int) v.getTag();
                adapter.setSelectIndex(p);
                adapter.notifyDataSetChanged();
                if (onSelectCategory != null) {
                    onSelectCategory.onSelectStatus(status.get(p), statusList.get(p));
                    dismiss();
                }
            }
        });
    }


    @OnClick(R2.id.view)
    void onClickOutSide() {
        dismiss();
    }


    //显示
    public void showPopupWindow(View parent) {
        if (!this.isShowing()) {
            this.showAsDropDown(parent, 0, 4);
        } else {
            this.dismiss();
        }
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if (Build.VERSION.SDK_INT == 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

    public void setOnSelectCategory(OnSelectStatus onSelectCategory) {
        this.onSelectCategory = onSelectCategory;
    }

    public interface OnSelectStatus {
        void onSelectStatus(Integer status, String stastr);
    }


}



